﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Models;

namespace VehicleMaintenance.BaseInfo
{
    public partial class OtherExpenditureListForm : ListBaseForm
    {

        public OtherExpenditureListForm()
        {
            InitializeComponent();
            BindColumn();
            this.IsSelect = true;
        }

        public OtherExpenditureListForm(bool select)
        {
            InitializeComponent();
            BindColumn();
            this.IsSelect = select;
        }


        private void OtherExpenditureListForm_Load(object sender, EventArgs e)
        {
            try
            {
                BindData();
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                OtherExpenditureForm form = new OtherExpenditureForm();
                form.ShowDialog(this);

                BindData();
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                if (lvData.SelectedItems.Count > 0)
                {
                    OtherExpenditureForm form = new OtherExpenditureForm(lvData.SelectedItems[0].Text);
                    form.ShowDialog(this);

                    BindData();
                }
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (lvData.SelectedItems.Count > 0)
                {
                    if (MessageBox.Show(this, "是否删除当月其他支出信息？", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    {
                        CarDBContext db = new CarDBContext();

                        string sql = "delete from OtherExpenditure where date=@date";
                        MySql.Data.MySqlClient.MySqlParameter parameter = new MySql.Data.MySqlClient.MySqlParameter();
                        parameter.DbType = DbType.String;
                        parameter.Direction = ParameterDirection.Input;
                        parameter.ParameterName = "@date";
                        parameter.Size = 10;
                        parameter.Value = lvData.SelectedItems[0].SubItems[0].Text.Trim();
                        db.Database.ExecuteSqlCommand(sql, parameter);

                        db.SaveChanges();

                        BindData();
                    }
                }
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        private void BindData()
        {
            try
            {
                lvData.Items.Clear();

                CarDBContext db = new CarDBContext();
                var list = db.OtherExpenditure.OrderByDescending(p => p.Date);

                foreach (var item in list)
                {                  
                    if (!lvData.Items.ContainsKey(item.Date))
                    {
                        ListViewItem lvItem = new ListViewItem(new string[lvData.Columns.Count]);
                        lvItem.Name = item.Date;
                        lvItem.Text = item.Date;
                        lvItem.SubItems[0].Text = item.Date;
                        lvData.Items.Add(lvItem);

                        if (lvData.Columns.ContainsKey("单位"))
                        {
                            lvItem.SubItems[lvData.Columns["单位"].Index].Text = item.Units;
                        }
                        if (lvData.Columns.ContainsKey("备注"))
                        {
                            lvItem.SubItems[lvData.Columns["备注"].Index].Text = item.Remark;
                        }
                    }

                    if (lvData.Columns.ContainsKey(item.Type))
                    {
                        lvData.Items[item.Date].SubItems[lvData.Columns[item.Type].Index].Text = item.Amount.ToString("f1");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void lvData_DoubleClick(object sender, EventArgs e)
        {
            if (lvData.SelectedItems.Count > 0)
            {
                if (IsSelect)
                {

                }
                else
                {
                    this.btnEdit_Click(sender, e);
                }
            }
        }

        private void BindColumn()
        {
            string[] strType = System.Configuration.ConfigurationManager.AppSettings["OtherExpenditureType"].Split(',');

            //生成支出种类列
            for (int i = 0; i < strType.Length; i++)
            {
                ColumnHeader header = new ColumnHeader();
                header.Text = strType[i];
                header.Width = 90;
                header.TextAlign = HorizontalAlignment.Right;
                header.Name = strType[i];

                lvData.Columns.Add(header);
            }

            //生成单位列
            ColumnHeader header1 = new ColumnHeader();
            header1.Text = "单位";
            header1.Width = 50;
            header1.TextAlign = HorizontalAlignment.Center;
            header1.Name = "单位";
            lvData.Columns.Add(header1);

            //生成备注列
            ColumnHeader header2 = new ColumnHeader();
            header2.Text = "备注";
            header2.Width = 150;
            header2.TextAlign = HorizontalAlignment.Left;
            header2.Name = "备注";
            lvData.Columns.Add(header2);
        }
    }
}
